/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.app;

import java.util.*;





import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;
import com.qbooks.app.AppDAO;
import com.qbooks.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class DownloadPreviewCommand implements Command
{
	private final String D = "\"";

    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	String os	= (String)request.getAttribute("os");


    	String bookCode	= (String)request.getParameter("book_code");
    	String mac		= (String)request.getParameter("mac");

    	HashMap infos 	= new HashMap();
    	infos.put("book_code", bookCode);
		infos.put("os_type", os);
		infos.put("mac", mac);

		AppDAO dao 		= new AppDAO();

		HashList list 	= dao.selectBook(infos);

		String book_url = "";
		int result_code = 100;

		if (list.size() == 1) {
			book_url = list.get("book_preview_url", 0);
			if ("".equals(book_url)) {
				result_code = 302;	//존재하지 않는 무료샘플
			} 
		} else {
			result_code = 302;	//존재하지 않는 책
		}


		if (result_code == 100) {
			infos.put("download_type", "2");	//미리보기
			dao.insertBookDownload(infos);
		}

		
		
		if ("android".equals(os)) {
			JSONObject json = new JSONObject();

			json.put("if_code", "DNP");
			json.put("result_code", result_code);
			if (result_code == 100) {
				json.put("book_url", book_url);
				json.put("file_length", list.get("preview_file_length", 0));
			}

			request.setAttribute("jsonData", json.toString());
		}
		else if ("ios".equals(os)) {
			request.setAttribute("jsonData", "{\"status\", \""+result_code+"\"}");
		}
		
		return "/common/json_view.jsp";
    }
}
